package com.boruan.qq.zxgylibrary.service.manager;

import android.content.Context;
import android.util.Log;

import com.boruan.qq.zxgylibrary.BuildConfig;
import com.boruan.qq.zxgylibrary.constants.ConstantInfo;
import com.boruan.qq.zxgylibrary.service.model.AddressBackPriceBean;
import com.boruan.qq.zxgylibrary.service.model.AddressListBean;
import com.boruan.qq.zxgylibrary.service.model.AppInfo;
import com.boruan.qq.zxgylibrary.service.model.DownOrderBean;
import com.boruan.qq.zxgylibrary.service.model.FirstBannerBean;
import com.boruan.qq.zxgylibrary.service.model.FirstCategoryBean;
import com.boruan.qq.zxgylibrary.service.model.PayResultBean;
import com.boruan.qq.zxgylibrary.service.model.SettleDefaultAddressBean;
import com.boruan.qq.zxgylibrary.service.model.ShopCarDataBean;
import com.convenient.qd.core.user.UserDBHelper;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;


/**
 * Created by 崔琦 on 2019/9/2.
 * des: 接口管理类,暂时先不用,统一改成国信的。
 */

public class DataManager {

    private RetrofitService mRetrofitService;

    public DataManager(Context context) {
        mRetrofitService = RetrofitHelper.getInstance(context).getService();
    }

    public DataManager(Context context,String msg) {
        mRetrofitService = GXRetrofitHelper.getInstance(context).getService();
    }

    public Observable<FirstBannerBean> getFirstBanner(){
        return mRetrofitService.getFirstBanner(ConstantInfo.userToken,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem());
    }

    public Observable<FirstCategoryBean> getFirstCategoryData(){
        return mRetrofitService.getFirstCategory(ConstantInfo.userToken,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem());
    }

    public Observable<ResponseBody> saveUpdateLocation(String lat,String lng){
        return mRetrofitService.saveUpdateLocation(ConstantInfo.userToken,lat,lng,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem());
    }

    public Observable<ResponseBody> addAddress(String userName,String userPhone,String address,String detailAddress,String fullAddress,int isDefault,int sex,String cityName,int tag){
        Map map = new HashMap();
        map.put("contactperson",userName);
        map.put("contactphone",userPhone);
        map.put("address",address);
        map.put("fulladdress",fullAddress);
        map.put("attachaddress",detailAddress);
        map.put("isdefault",isDefault);
        map.put("sex",sex);
        map.put("cityName",cityName);
        map.put("tag",tag);
        map.put("appInfo",new AppInfo());
        String json = new Gson().toJson(map);
        Log.i("json",json);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"),json);
        return mRetrofitService.addAddress("Bearer " + UserDBHelper.getInstance().getAccessToken(),body);
    }

    public Observable<AddressListBean> getAddressData(String shopId){
        return mRetrofitService.getAddressData(ConstantInfo.userToken,shopId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem());
    }

    public Observable<ResponseBody> deleteAddress(int addressId){
        return mRetrofitService.deleteAddress(ConstantInfo.userToken,addressId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem());
    }

    public Observable<ResponseBody> modifyAddress(int addressId,int sex,String userName,String userPhone,String address,String detailAddress,String fullAddress,int isDefault,String cityName,int tag){
        Map map = new HashMap();
        map.put("id",addressId);
        map.put("sex",sex);
        map.put("contactperson",userName);
        map.put("contactphone",userPhone);
        map.put("address",address);
        map.put("fulladdress",fullAddress);
        map.put("attachaddress",detailAddress);
        map.put("isdefault",isDefault);
        map.put("cityName",cityName);
        map.put("tag",tag);
        map.put("appInfo",new AppInfo());
        String json = new Gson().toJson(map);
        Log.i("json",json);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"),json);
        return mRetrofitService.modifyAddress(ConstantInfo.userToken,body);
    }

    public Observable<ResponseBody> modifyDefaultAddress(int addressId){
        return mRetrofitService.modifyDefaultAddress(ConstantInfo.userToken,addressId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem());
    }

    public Observable<ShopCarDataBean> getShopCarData(String shopId){
        return mRetrofitService.getShopCarData(ConstantInfo.userToken,shopId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem());
    }

    public Observable<SettleDefaultAddressBean> getSettleDefaultAddress(String shopId){
        return mRetrofitService.getSettleDefaultAddress(ConstantInfo.userToken,shopId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem());
    }

    public Observable<AddressBackPriceBean> judgeDelivery(int addressId,String shopId){
        return mRetrofitService.judgeDelivery(ConstantInfo.userToken,addressId,shopId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem());
    }

    public Observable<DownOrderBean> userDownOrder(int creditToMoney, int deliveryType, int distance, int freight, int addressId, int paymentPrice, String shopId, ShopCarDataBean shopCarDataBean, String remark){
        Map map = new HashMap();
        map.put("creditToMoney",creditToMoney);
        map.put("deliveryType",deliveryType);
        map.put("distance",distance);
        map.put("freight",freight);
        map.put("addressId",addressId);
        map.put("paymentPrice",paymentPrice);
        map.put("shopId",shopId);
        map.put("orderProductDetails",shopCarDataBean.getResult().get(0).getShopCartSkuDTOList());
        map.put("memo",remark);
        map.put("appInfo",new AppInfo());
        String json = new Gson().toJson(map);
        Log.i("json",json);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"),json);
        return mRetrofitService.userDownOrder(ConstantInfo.userToken,body);
    }

    public Observable<ResponseBody> getOrderDetailInfo(String orderId){
        return mRetrofitService.getOrderDetailInfo(ConstantInfo.userToken,orderId,new AppInfo().getBundleId(),new AppInfo().getAppVersion(),new AppInfo().getSourceSystem());
    }

    public Observable<ResponseBody> payNotice(String orderId, String wxPay){
        Map<String, RequestBody> requestBodyMap = new HashMap<>();
        Map<String, String>  requestDataMap = new HashMap();
        requestDataMap.put("merOrderId",orderId);
        requestDataMap.put("targetSys",wxPay);
        requestDataMap.put("bundleId",new AppInfo().getBundleId());
        requestDataMap.put("appVersion",new AppInfo().getAppVersion());
        requestDataMap.put("sourceSystem",new AppInfo().getSourceSystem());
        for (String key : requestDataMap.keySet()) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                    requestDataMap.get(key) == null ? "" : requestDataMap.get(key));
            requestBodyMap.put(key, requestBody);
        }
        return mRetrofitService.payNotice(ConstantInfo.userToken,requestBodyMap);
    }

    public Observable<ResponseBody> getPayParam(String orderId, String wxPay){
//        Map<String, RequestBody> requestBodyMap = new HashMap<>();
        Map<String, Object>  requestDataMap = new HashMap();
        Map<String, String>  appInfo = new HashMap();
        appInfo.put("bundleId","com.bsit.coband");
        appInfo.put("appVersion", BuildConfig.VERSION_NAME);
        appInfo.put("sourceSystem","1");

        requestDataMap.put("tradeId",orderId);
        requestDataMap.put("payWay",wxPay);
        requestDataMap.put("appId","AP577892496042885120");
        requestDataMap.put("accessToken",ConstantInfo.h5UserToken);
        requestDataMap.put("appInfo",appInfo);

//        for (String key : requestDataMap.keySet()) {
//            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
//                    requestDataMap.get(key) == null ? "" : requestDataMap.get(key));
//            requestBodyMap.put(key, requestBody);
//        }
        return mRetrofitService.getPayParam(ConstantInfo.userToken,requestDataMap);
    }

    public Observable<PayResultBean> getPayResultInfo(String tradeId){
        Map map = new HashMap();
        map.put("tradeId",tradeId);
        map.put("appId","AP577892496042885120");
        map.put("accessToken",ConstantInfo.h5UserToken);
        map.put("appInfo",new AppInfo());
        String json = new Gson().toJson(map);
        Log.i("json",json);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=utf-8"),json);
        return mRetrofitService.getPayResultInfo(ConstantInfo.userToken,body);
    }
}
